package com.plants.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plants.back.pojo.PlantsGoods;
import com.plants.back.pojo.PlantsOrders;
import com.plants.back.pojo.PlantsShop;
import com.plants.back.pojo.Vo;
import com.plants.back.service.impl.PlantsOrdersServiceImpl;
import com.plants.back.service.impl.PlantsShopServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  订单管理控制器
 * </p>
 *
 * @author plants
 * @since 2024-08-06
 */
@CrossOrigin
@RestController
@RequestMapping("/plants_orders")
public class PlantsOrdersController {

    @Resource
    private PlantsOrdersServiceImpl plantsOrdersService;

    @Resource
    private PlantsShopServiceImpl shopService;

    // 新增订单
    @PostMapping("/insert")
    public Vo<PlantsOrders> insert(@RequestBody PlantsOrders orders) {
        // 设置成为本地时间
        orders.setDataTime(LocalDateTime.now());
        boolean b = plantsOrdersService.save(orders);
        if (b) {
            // 新增成功
            return new Vo<>(b, "购买成功！");
        } else {
            // 新增失败
            return new Vo<>(b, "添加失败，请稍后重试！");
        }
    }
    // 删除订单
    @GetMapping("/delete/{id}")
    public Vo<PlantsOrders> delete(@PathVariable("id") String id) {
        UpdateWrapper<PlantsOrders> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean b = plantsOrdersService.remove(wrapper);
        if (b) {
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }
    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<PlantsOrders> page(@PathVariable("current") Integer current) {
        Page<PlantsOrders> ordersPage = new Page<>(current <= 0 ? 1 : current, 6);
        IPage<PlantsOrders> page = plantsOrdersService.page(ordersPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有订单！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }
    // 搜索查询
    @GetMapping("/search/{name}/{current}")
    public Vo<PlantsOrders> search(@PathVariable("name") String name, @PathVariable("current") Integer current) {
        Page<PlantsOrders> ordersPage = new Page<>(current <= 0 ? 1 : current, 6);
        LambdaQueryWrapper<PlantsOrders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PlantsOrders::getGoodsName, name);
        Page<PlantsOrders> page = plantsOrdersService.page(ordersPage, wrapper);
        if (page.getTotal() == 0) {
            // 表示没有数据
            return new Vo<>(true, "目前没有数据");
        } else {
            return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
        }
    }

    // 购物车付款
    @PostMapping("/insert_shop")
    public Vo<PlantsOrders> insertShop(@RequestBody PlantsOrders orders) {
        UpdateWrapper<PlantsShop> wrapper = new UpdateWrapper<>();
        wrapper.eq("u_id",orders.getUId());
        wrapper.eq("g_id",orders.getGId());
        boolean flag = shopService.remove(wrapper);
        if (flag){
            // 设置成为本地时间
            orders.setDataTime(LocalDateTime.now());
            boolean b = plantsOrdersService.save(orders);
            if (b) {
                // 新增成功
                return new Vo<>(b, "");
            } else {
                // 新增失败
                return new Vo<>(b, "");
            }
        }else {
            return new Vo<>(false,"");
        }
    }

}
